package com.lsk.mobile;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.jfinal.plugin.activerecord.Db;
import com.lsk.alipay.constants.AlipayServiceEnvConstants;

import java.util.Map;

public class AlipaySubmit {

    /**
     * 获取AccessToken
     * @param sParaTemp
     * @return
     */
    public static  String buildRequest(Map sParaTemp) throws AlipayApiException{
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        String sql="SELECT `value` from sys_config where element=";

        String applyAppId= Db.queryStr(sql+"'limoAppId'");     //@TODO 更换

        //String applyPublicKey= AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY;     //@TODO更换

        String applyPublicKey=Db.queryStr(sql+" 'limoAppPublicKey'");
        String applyPrivateKey= Db.queryStr(sql+"'limoAppPrivateKey'");       //@TODO更换

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        applyAppId, applyPrivateKey, "json", "GBK",
                        applyPublicKey, "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode((String)sParaTemp.get("auth_code"));
        request.setGrantType("authorization_code");
        AlipaySystemOauthTokenResponse oauthTokenResponse=new AlipaySystemOauthTokenResponse();
        try {
             oauthTokenResponse = alipayClient.execute(request);
            return oauthTokenResponse.getAccessToken();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }finally {
        }
        return null;
    }

    /**
     * 获取用户信息
     * @param accessToken
     * @return
     */
    public static String get(String accessToken){
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        String sql="SELECT `value` from sys_config where element=";

        String applyAppId= Db.queryStr(sql+"'applyAppId'");

        String applyPublicKey= AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY;


        String applyPrivateKey= Db.queryStr(sql+"'applyPrivateKey'");

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        applyAppId, applyPrivateKey, "json", "GBK",
                        applyPublicKey, "RSA2");
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();

        try {
            AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(request, accessToken);
            return  userinfoShareResponse.getBody();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }
        return null;
    }

}
